package com.eke.dao;

import java.util.Map;

import org.springframework.stereotype.Repository;
import com.eke.bean.UserConcern;
import com.jason.dbhelper2.jdbc.BaseJdbcTemplate;

@Repository("userConcernDao")
public class UserConcernDao extends BaseJdbcTemplate<UserConcern> {
	/**
	 * 查询用户被关注量
	 * @param useConcernId 用户id
	 * @return int
	 */
	public int qurryUserConcernTotal(int userConcernId) {
		String sql = "select count(1) total from user_concern WHERE use_concern_id=?";
		Map<String, Object> map = this.getJdbcTemplate().queryForMap(sql, userConcernId);
		return ((Long) map.get("total")).intValue();
	}
	
	/**
	 * 查询用户关注量
	 * @param useId 用户id
	 * @return int
	 */
	public int qurryUserConcernTotalByUserId(int userId) {
		String sql = "select count(1) total from user_concern WHERE user_id=?";
		Map<String, Object> map = this.getJdbcTemplate().queryForMap(sql, userId);
		return ((Long) map.get("total")).intValue();
	}
	
	/**
	 * 关注用户
	 * @param userId
	 * @param userConcernId
	 * @return {@link Boolean}
	 */
	public Boolean addUserConcern(int userId,int userConcernId) {
		UserConcern uConcern=new UserConcern();
		String sql="select * from user_concern where user_id=? and use_concern_id=?";
		if (this.queryForList(sql, userId,userConcernId).size()>1) {
			uConcern.setUserId(userId);
			uConcern.setUseConcernId(userConcernId);
			if (this.insert(uConcern)>0) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
	
	/**
	 * 是否关注用户
	 * @param userId
	 * @param userConcernId
	 * @return {@link Boolean}
	 */
	public Boolean isAddUserConcern(int userId,int userConcernId) {
		String sql="select * from user_concern where user_id=? and use_concern_id=?";
		if (this.queryForList(sql, userId,userConcernId).size()>0) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * 取消用户
	 * @param userId
	 * @param userConcernId
	 * @return {@link Boolean}
	 */
	public Boolean cencelUserConcern(int userId,int userConcernId) {
		String sql="DELETE  FROM user_concern WHERE user_id = ? AND use_concern_id = ?";
		if (this.update(sql, userId,userConcernId)>0) {
			return true;
		} else {
			return false;
		}
	}
}
